﻿using MVC_No06.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No06.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(string selectListItemsByUid = "", string Content = "")//显示页面
        {
            using (Model1 model = new Model1())
            {
                List<Task> listTask = model.Task.Where(u => true).ToList();
                if (!string.IsNullOrEmpty(Content))
                {
                    listTask = listTask.Where(u => u.Content == Content).ToList();
                }
                if (!string.IsNullOrEmpty(selectListItemsByUid) && int.TryParse(selectListItemsByUid, out int j))
                {
                    var id = Convert.ToInt32(selectListItemsByUid);
                    if (id != 0)
                        listTask = listTask.Where(u => u.uid == id).ToList();
                }

                //uid置换
                Dictionary<int, string> dict = new Dictionary<int, string>();
                var data = model.userinfo.Where(u => true).ToList();
                foreach (var item in data)
                    dict.Add(item.uid, item.name);
                ViewData["userInfo"] = dict;
                //载入下拉框
                var uidList = model.userinfo.Where(u => true).ToList();
                List<SelectListItem> selectListItemsByUidList = new List<SelectListItem>() {
                    new SelectListItem(){ Value = "0",Text="全部",Selected = true},
                };
                for (int i = 0; i < uidList.Count; i++)
                {
                    selectListItemsByUidList.Add(new SelectListItem() { Value = uidList[i].uid.ToString(), Text = uidList[i].name, Selected = true });
                }
                ViewBag.selectListItemsByUid = new SelectList(selectListItemsByUidList, "Value", "Text", "Selected");

                return View(listTask);
            }
        }

        [HttpPost]
        public ActionResult IndexOfDropDownList()
        {
            return Json(new { state = 1 });
            using (Model1 model = new Model1())
            {
                var list = model.userinfo.Where(u => true).ToList();
                Dictionary<int, string> dict = new Dictionary<int, string>();
                foreach (var item in list)
                {
                    dict.Add(item.uid, item.name);
                }

                //return Json(dict);
                return Json(new { state = 1 });
            }
        }

        [HttpGet]
        public ActionResult Create()//添加页面
        {
            using (Model1 model = new Model1())
            {
                //获取UID
                var uidList = model.userinfo.Where(u => true).ToList();
                List<SelectListItem> selectListItemsByUid = new List<SelectListItem>();
                for (int i = 0; i < uidList.Count; i++)
                {
                    selectListItemsByUid.Add(new SelectListItem() { Value = uidList[i].uid.ToString(), Text = uidList[i].name });
                }
                ViewBag.TaskUid = selectListItemsByUid;

                //获取优先级
                List<SelectListItem> selectListItemsByTaskPriority = new List<SelectListItem>() {
                    new SelectListItem(){ Value = "1",Text="高"},
                    new SelectListItem(){ Value = "2",Text="中"},
                    new SelectListItem(){ Value = "3",Text="低"},
                };
                ViewBag.taskPriorityByBag = selectListItemsByTaskPriority;
            }
            return View();
        }

        [HttpPost]
        public ActionResult CreateTask(Task task)//添加
        {
            if (!ModelState.IsValid)
                return Json(null);
            using (Model1 model = new Model1())
            {
                task.AddTime = DateTime.Now;
                task.TaskState = 1;

                model.Task.Add(task);

                if (model.SaveChanges() == 1)
                    return RedirectToAction("index", "MyHome");
            }
            return RedirectToAction("Create", "MyHome");
        }

        [HttpGet]
        public ActionResult Update(string uid = null)//修改页面
        {
            if (!string.IsNullOrEmpty(uid))
            {
                if (int.TryParse(uid, out int j))
                {
                    int id = Convert.ToInt32(uid);
                    using (Model1 model = new Model1())
                    {
                        var oldTask = model.Task.FirstOrDefault(u => u.TaskID == id);
                        //获取优先级
                        List<SelectListItem> selectListItemsByTaskPriority = new List<SelectListItem>() {
                            new SelectListItem(){ Value = "1",Text="高"},
                            new SelectListItem(){ Value = "2",Text="中"},
                            new SelectListItem(){ Value = "3",Text="低"},
                        };
                        selectListItemsByTaskPriority[Convert.ToInt32(oldTask.TaskPriority) - 1].Selected = true;
                        ViewBag.TaskPriorityByBag = new SelectList(selectListItemsByTaskPriority, "Value", "Text", "Selected");

                        //完成度
                        List<SelectListItem> selectListItemsByTaskState = new List<SelectListItem>() {
                            new SelectListItem(){ Value = "1",Text="未完成"},
                            new SelectListItem(){ Value = "2",Text="已完成"},
                        };
                        selectListItemsByTaskState[Convert.ToInt32(oldTask.TaskState) - 1].Selected = true;
                        ViewBag.TaskStateByBag = new SelectList(selectListItemsByTaskState, "Value", "Text", "Selected");

                        //获取UID
                        var uidList = model.userinfo.Where(u => true).ToList();
                        List<SelectListItem> selectListItemsByUid = new List<SelectListItem>();
                        for (int i = 0; i < uidList.Count; i++)
                        {
                            selectListItemsByUid.Add(new SelectListItem() { Value = uidList[i].uid.ToString(), Text = uidList[i].name });
                            if (oldTask.uid.ToString() == selectListItemsByUid[i].Value)
                            {
                                selectListItemsByUid[i].Selected = true;
                                //UID的默认值添加不上?
                            }
                        }
                        ViewBag.selectListItemsByUid = new SelectList(selectListItemsByUid, "Value", "Text", "Selected");


                        //ViewBag.selectListItemsByUid = new SelectList(
                        //    new List<userinfo>() {
                        //        new userinfo(){uid = 1,name = "张三" },
                        //        new userinfo(){uid = 2,name = "李四" },
                        //    }, "uid", "name");

                        return View(oldTask);
                    }
                }
            }
            return RedirectToAction("index", "MyHome");
        }

        [HttpPost]
        public ActionResult UPdateTask(Task task)//修改
        {
            if (ModelState.IsValid)
            {
                using (Model1 model = new Model1())
                {
                    var oldTask = model.Task.FirstOrDefault(u => u.TaskID == task.TaskID);
                    if (oldTask.TaskState == 1 && task.TaskState == 2)//未完成变已完成
                        oldTask.FinishedTime = DateTime.Now;
                    if (oldTask.TaskState == 2 && task.TaskState == 1)//已完成变未完成
                        oldTask.FinishedTime = null;

                    oldTask.TaskPriority = task.TaskPriority;
                    oldTask.Content = task.Content;
                    oldTask.TaskState = task.TaskState;
                    oldTask.uid = task.uid;

                    if (model.SaveChanges() == 1)
                        return RedirectToAction("index", "MyHome");
                    else
                        return Json(null);
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult Delete(string uid = null)//删除
        {
            if (!string.IsNullOrEmpty(uid))
            {
                if (int.TryParse(uid, out int j))
                {
                    int id = Convert.ToInt32(uid);
                    using (Model1 model = new Model1())
                    {
                        model.Task.Remove(model.Task.FirstOrDefault(u => u.TaskID == id));
                        return Json(model.SaveChanges());
                    }
                }
            }
            return Json(3);
        }
    }
}